Flash Article

Updating a Database Using Flash MX Professional 2004


Table of Contents

  1. Introduction
  2. The Basics
  3. Update Packets in Detail
  4. Result Packets in Detail
  5. A Sample Update Packet Parser
  6. Setting up the ColdFusion Component
  7. Building the Flash Application
  8. Summary

Result Packets in Detail

A result packet is an XML document that contains the results of the operations in the corresponding update packet. A result packet can provide a simple response to the DataSet component, providing feedback that all operations were performed successfully, or it can be more complex and include error messages and information about changed field values.

The following example shows a simple result packet:

<results_packet transID="46386292065"/>

By returning the transID from the update packet, the result packet informs the DataSet component that the update packet was received and processed. No additional information needs to be sent when there are no errors and no fields to update.

A more elaborate result packet (with both errors and field change information) might look like this:

<results_packet transID="46386292065">
    <operation op="delete" id="11295627479" msg="Record not found" />
    <operation op="update" id="02938027477">
        <field name="id" msg="Invalid field value" />
    </operation>
    <operation op="insert" id="02938027470">
        <field name="id" curValue="105034" />
    </operation>
</results_packet>

This packet shows the results of an update packet where two of the operations had errors. The third operation succeeded, but a field was updated by the server function (I'll discuss this shortly).

Underneath the top level <results_packet> node, there can be any number of <operation> nodes. Each <operation> node must have an op attribute with the value of insert, delete, or update, and an id attribute that holds the ID of the operation from the update packet. If the operation had an error, the node should also have a msg attribute that contains the error message text.

Each <operation> node can contain an unlimited number of <field> nodes. These nodes should have a name attribute that contains the field name, a msg attribute to flag field-level error messages, and a curValue attribute that indicates the field's most current value on the server.

A non-empty msg attribute in any <operation> or <field> node tells the DataSet component that the operation that contains the msg attribute failed, and provides the specific reason for the failure.

Updating Fields on the Server

As I mentioned above, the result packet can contain updated field values. Why? One usage is to allow for scenarios where new records are submitted to a server, and a function on the server sets the primary key value for the new record. This is often the case in multiuser systems. Since these updated values can be returned in the result packet, the DataSet component can be notified of these new values without having to make another request for the same data.

In the sample results packet above, the final <operation> node is an example of this scenario. The node indicates that the insert operation was performed successfully, and that the new record's id field was set to 105034. When that result is received by the DataSet component, it locates the record identified by the operation's id attribute, and updates the record with that value. Any subsequent update packets that contain edits of that record correctly identify the record as having an id field with a value of 105034.

Transaction IDs and Operation IDs

Transaction IDs and operation IDs are used by the DataSet for bookkeeping. They help it track records sent to and received from the server. For ease of display, I have shortened the transaction ID value. The actual value is much longer and includes a timestamp to help make it unique. When parsing the update packet, it's important to retain these values and return them in the result packet.

The DataSet produces a DeltaPacket when you call its applyUpdates() method. If results from the previous DeltaPacket have not yet been returned, the DataSet will still produce a new DeltaPacket containing only the data that has been changed since the last one. So a DataSet at times can be waiting for results from two or more DeltaPackets. You can see how important it is to return these IDs.

How the DataSet Handles Errors

When the DataSet receives results, in addition to updating any fields that were modified on the server, it looks for any operations that returned errors. Each operation with an error is placed back into the queue to go out in the next DeltaPacket, and each error is put into an array. The DataSet then fires its onResolveDelta event, making that error array available as the data property of the dispatched event. A simple example of displaying errors is included in the sample Flash application.